<?php

namespace Encore\ImageExt\Http\Controllers;


use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\ImageExt\Models\Media;
use Encore\Admin\Controllers\AdminController as Controller;
use Illuminate\Http\Request;

class ImageExtController extends Controller
{

    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = 'Media';


    public function index(Content $content)
    {
        return $content
            ->title($this->title)
            ->description('文件信息列表')
            ->body($this->grid());
    }

    public function show($id, Content $content)
    {
        return $content
            ->title($this->title)
            //->description($this->description['show'] ?? trans('admin.show'))
            ->body($this->detail($id));
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Media());
        $grid->column('id')->sortable();

        $userModel = config('auth.providers.users.model');
        //$grid->column('id', __('Id'));
        $grid->column('user_id', '用户')->display(function ($id) use ($userModel){
            $user = $userModel::find($id);
            return $user?$user->nickname:'-';
        })->help('-代表该文件无归属')->editable()->sortable();
        $grid->column('tag', '标签')->help("默认admin后台上传")->editable()->sortable();
        $grid->column('type', '类型')->sortable();
        //$grid->column('bucket', '存储桶');
        //$grid->column('disk', '磁盘');
        $grid->column('link','文件路径')->link();
        $grid->column('size', '文件大小(K)')->sortable();
        //$grid->column('file_ext', '文件后缀');
        //$grid->column('file_name', '文件名');
        //$grid->column('meta', '文件属性');
        $grid->column('created_at', '上传时间');
        //$grid->column('updated_at', __('Updated at'));
        //$grid->column('deleted_at', __('Deleted at'));

        $grid->disableCreateButton();
        $grid->actions(function ($actions){

            $actions->disableEdit();
            $actions->append('<a href=""><i class="fa fa-eye"></i></a>');
            // 去掉查看
            $actions->disableView();
        });

        $grid->filter(function($filter){
            $filter->disableIdFilter();
            $filter->equal('user_id', '昵称')->select('ajax/userlist');
            $filter->equal('tag','标签')->placeholder('请输入。。。');;
            $filter->like('path', '路径');

        });


        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Media::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('user_id', '用户ID');
        $show->field('tag', '标签');
        $show->field('type', '类型');
        //$show->field('bucket', '存储桶')->default('no');
        //$show->field('disk', '磁盘');
        $show->field('path','文件路径');
        $show->field('size', '文件大小(K)');
        $show->field('file_ext', '文件后缀');
        $show->field('file_name', '文件名');
        //$show->field('meta', '文件属性');
        $show->field('created_at', '上传时间');
        //$show->field('updated_at', __('Updated at'));
        //$show->field('deleted_at', __('Deleted at'));

        return $show;
    }

    public function create(Content $content){
        return $content->withError("forbidden", "Not Allowed Create");
    }

    public function update($id)
    {
        // {"status":true,"message":"\u66f4\u65b0\u6210\u529f !","display":[]}
        $request = Request::capture();
        $field = $request->get('name');
        $value = $request->get('value');

        if (!array($field, ['user_id','tag'])) {
            return $this->response(false, '不支持更新此字段');
        }else{
            $model = Media::find($id);
            if ($field == 'user_id') {
                $userModel = config('auth.providers.users.model');
                $user = $userModel::where('nickname', $value)->first();
                if ( !$user) {
                   return $this->response(false,'请输入有效的用户昵称');
                }
                $model->user_id = $user->id;
            }
            if ($field == 'user_tag') {
                if ( !trim($value) ){
                    return $this->response(false,'请输入正确的标签名');
                }
                $model->tag = $value;
            }
            $model->save();
            return $this->response(true, '操作成功');
        }
    }

    protected function response($status, $message){
        return response()->json([
            'status'     => $status,
            'validation' => "error",
            'message'    => $message,
        ]);
    }
}
